#include "logui.h"
#include "ui_logui.h"
#include "QGroupBox"
#include <QBitmap>
#include <QGraphicsDropShadowEffect>
#include <QPainter>
#include <QStyleOption>
#include <QVBoxLayout>
#include <qlabel.h>
#include <qtextcodec.h>

LogUI::LogUI(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::LogUI)
{
    ui->setupUi(this);
    //隐藏标题栏
    setWindowFlags(Qt::FramelessWindowHint|Qt::Tool|Qt::WindowStaysOnTopHint);

       //外层窗口显示为透明
    //setAttribute(Qt::WA_TranslucentBackground,true);
    //设置窗体透明
    this->setAttribute(Qt::WA_TranslucentBackground, true);
    //设置无边框
    this->setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
    //实例阴影shadow
    QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect(this);
    shadow->setOffset(0, 0);
    //设置阴影颜色
    shadow->setColor(QColor("#444444"));
    //设置阴影长度
    shadow->setBlurRadius(20);
//    给嵌套QWidget设置阴影
    ui->in_widget->setGraphicsEffect(shadow);



    /** 美化scrollArea **/
    ui->scrollArea->setStyleSheet("background-color: blue");
    ui->scrollArea->setStyleSheet("border-radius:8px;");
    //设置具体阴影
    QGraphicsDropShadowEffect *shadow_effect = new QGraphicsDropShadowEffect(this);
    shadow_effect->setOffset(0, 0);
    //阴影颜色
    shadow_effect->setColor(QColor(38, 78, 119, 127));
    //阴影半径
    shadow_effect->setBlurRadius(22);
    ui->scrollArea->setGraphicsEffect(shadow_effect);

    /** 美化label **/
    ui->label->setStyleSheet("background-color: rgb(0, 0, 0);");
    ui->label->setStyleSheet("border-radius:8px;");
    //设置具体阴影
    QGraphicsDropShadowEffect *shadow_effect1 = new QGraphicsDropShadowEffect(this);
    shadow_effect1->setOffset(0, 0);
    //阴影颜色
    shadow_effect1->setColor(QColor(38, 78, 119, 127));
    //阴影半径
    shadow_effect->setBlurRadius(22);
    ui->label->setGraphicsEffect(shadow_effect1);


//    QWidget* widget = new QWidget;
//    QVBoxLayout *layout = new QVBoxLayout;  //设置垂直布局
//    widget->setLayout(layout);
//    ui->scrollArea->setWidget(widget);


    initLog();
}


bool fileExists(const std::string& filename)
{
    std::ifstream file(filename);
    return file.good();
}

void Log::createLog(string file){
    if (fileExists(file))
    {
        cout << "File exists!" << endl;
        filePath=file;
        return;
    }
    filePath=file;
    ofstream s;
    s.open(file,ios::out);
    s<<"LogFile"<<endl;
    s.close();
    cout<<"Create Success!"<<endl;
}

void Log::readLog(){
    fstream s;
    s.open(filePath,ios::in);
    if(!s.is_open()){
        cout<<"Failed to open file."<<endl;
    }
    string f;
        //一直读到文件末尾
       while(s>>f)
       cout<<f<<endl;
    s.close();

}


void Log::writeLog(string message)
{

    ofstream s;
    s.open(filePath,ios::app);
    if(!s.is_open()){
        cout<<"Failed to open file."<<endl;
    }

    time_t now = time(0); // 把 now 转换为字符串形式
    char* dt = ctime(&now);
    s<< endl;
    s<< dt;
    s << message;
    s.close();
}

LogUI::~LogUI()
{
    delete ui;
}
void LogUI::mousePressEvent(QMouseEvent *event)
{

     if( (event->button() == Qt::LeftButton) ){
         mouse_press = true;
         mousePoint = event->globalPos() - this->pos();
 //        event->accept();
    }
     else if(event->button() == Qt::RightButton){
         //如果是右键
        this->close();
     }
}
void LogUI::mouseMoveEvent(QMouseEvent *event)
{
 //    if(event->buttons() == Qt::LeftButton){  //如果这里写这行代码，拖动会有点问题
     if(mouse_press){
         this->move(event->globalPos() - mousePoint);
 //        event->accept();
     }
}
void LogUI::mouseReleaseEvent(QMouseEvent *event)
{
     mouse_press = false;
}

void LogUI::initLog(){

     ui->scrollArea->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));
     ui->scrollArea->setWidgetResizable(true);
     QVBoxLayout* qv = new QVBoxLayout(this);
     qv->setSizeConstraint(QVBoxLayout::SetMinAndMaxSize);
     QWidget* widget = new QWidget(this);
     widget->setMinimumSize(400, 200);
     widget->setMaximumSize(3000,300);
//     int i = 0;
//     QString str("pushButton %1");
//     QPushButton* pushButton;
//     for (i=0; i<50; ++i){
//        pushButton = new QPushButton(str.arg(i+1), widget);
//        pushButton->setMinimumSize(pushButton->size());
//        qv->addWidget(pushButton);
//     }




    QFile fileOpen(QApplication::applicationDirPath() + "/User'sTableInfo/lyyLog.txt");
    qDebug()<< QApplication::applicationDirPath() + "/User'sTableInfo/lyy/log.txt";
    if( fileOpen.open(QFile::ReadOnly | QIODevice::Text)){
        QTextStream in(&fileOpen); //创建输出流
        in.readLine();
        while(!in.atEnd())
        {
            QString data;
            data += in.readLine();
            data += "\n";
            data += in.readLine();
            data += "\n";
          QLabel* ql = new QLabel();
//          ql->setMargin(15);
          ql->setFixedSize(2000, 100);
          ql->setText(data);
          qv->addWidget(ql);
        }
    }
    qDebug()<< "读取";
    fileOpen.close();
    widget->setLayout(qv);
    ui->scrollArea->setWidget(widget);
}

QGroupBox* LogUI::addNew(QString time, QString instruction){




}



void LogUI::on_pushButton_clicked()
{
    close();
}
